﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using digitwhu.Models;
using digitwhu.Models.IRepositorys;
using digitwhu.Models.Repositorys;

namespace digitwhu.Logic.BusinessLogic
{
    public class BuildingLogic
    {
        #region 食堂相关
        public static int GetCanteenIdByBuildingId(int buildingId)//根据给定的建筑物id，得到自定义食堂编号
        {
            //buildingId:建筑物ID
            //返回值：食堂自定义编号,负数表示参数错误
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            return canteen.GetCanteenIdByBuildingId(buildingId);

        }
        public static List<int> GetCanWindowList(int canteenId) //获取某个食堂的所有窗口列表
        {
            //canteenId:预获取所有窗口的食堂的编号
            //返回值：食堂窗口的编号列表
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            return canteen.GetCanWindowList(canteenId);
        }
        public static double GetCanWindowGrade(int canWindowId)    //获取某个窗口的最终得分
        {
            //canWindowId:将要获取评分的窗口编号
            //返回值：食堂中某个窗口的最终得分
            //说明：得分按照去掉一个最高分，去掉一个最低分，然后去平均分的原则
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            List<int> grade = canteen.GetCanWindowGrade(canWindowId);
            int sum = 0;            //总分
            int count = 0;          //总共评分记录
            int highest = -1;        //最高分
            int lowest = 6;         //最低分
            foreach (int g in grade)
            {
                if (g > highest)
                {
                    highest = g;    //得到最高分
                }
                if (g < lowest)
                {
                    lowest = g;     //得到最低分
                }
                sum = sum + g;      //得到总分
                count++;    //评分总次数
            }
            if (count > 2)  //只有在count>2的情况下才算分
            {
                double avg = (sum - lowest - highest) / double.Parse((count - 2).ToString());    //得到最终得分
                return avg;
            }
            else
            {
                return 0;
            }
        }
        public static bool AddCanWindowGrade(int userId, int canWindowId, int grade)  //给某个窗口评分
        {
            //userId:要给窗口评分的用户编号
            //canWindowId:要评分的窗口编号
            //grade:用户给的分数(只能是0~5)
            //返回值：true:用户评分成功   false:用户评分失败
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            return canteen.AddCanWindowGrade(userId, canWindowId, grade);
        }
        public static List<CanMessage> GetCanMessageList(int canteenId)//获取某个食堂的所有留言
        {
            //canteenId:预获取留言的食堂编号
            //返回值：某个食堂的所有留言对象列表
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            return canteen.GetCanMessageList(canteenId);
        }
        public static bool AddCanMessage(int userId, int canteenId, string content)//给某个食堂留言
        {
            //userId:待发表留言的用户编号
            //canteenId:预被留言的食堂编号
            //content:留言内容
            //返回值：操作是否成功。true:发表留言成功    false:发表留言失败
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            return canteen.AddCanMessage(userId, canteenId, content);
        }
        public static CanMessage GetLatestCanMessage(int canteenId)//获得最新的食堂留言
        {
            //canteenId:食堂编号
            //返回值：null或者最新的CanMessage对象
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            return canteen.GetLatestCanMessage(canteenId);
        }
        public static List<int> GetCanRecommendList(int canteenId)//获取给定食堂的推荐美食列表
        {
            //canteenId:预获取食堂推荐美食的食堂编号
            //返回值：推荐美食记录的编号列表
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            return canteen.GetCanRecommendList(canteenId);
        }
        public static bool AddCanRecommendFood(int userId, int canteenId, string contents, string picture)//给食堂推荐食物 
        {
            //userId:要推荐美食的用户编号
            //canteenId:要推荐美食给指定食堂的食堂编号
            //contents:推荐美食文字内容
            //picture:推荐美食中的图片路径(默认只能为一条图片)
            //返回值：true：推荐美食成功 false:推荐美食失败
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            return canteen.AddCanRecommendFood(userId, canteenId, contents, picture);
        }
        public static bool DeleteCanRecommendFood(int recommendId)//删除自己曾经推荐的美食记录
        {
            //recommendId:将要删除的推荐美食记录编号
            //返回值：true：删除成功 false:删除失败
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            return canteen.DeleteCanRecommendFood(recommendId);
        }
        public static int GetLatestCanRecommendFoodId(int canteenId)//获取指定食堂最新的推荐美食编号
        {
            //canteenId:预获得最新推荐美食的食堂编号
            //返回值：正数表明最新的推荐美食记录编号，负数表示系统出现异常
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            return canteen.GetLatestCanRecommendFoodId(canteenId);
        }
        public static bool UpdateCanRecommendFood(int recommendId, string contents, string pictrue)//更新用户推荐的美食记录
        {
            //recommendId:推荐美食的ID
            //userId:要推荐美食的用户编号
            //canteenId:要推荐美食给指定食堂的食堂编号
            //contents:推荐美食文字内容
            //picture:推荐美食中的图片路径(默认只能为一条图片)
            //返回值：true：更改推荐美食成功 false:更改推荐美食失败
            DataContext dbContext = new DigitwhuDataContext();
            ICanteenRepository canteen = new CanteenRepository(dbContext);
            return canteen.UpdateCanRecommendFood(recommendId, contents, pictrue);
        }
        #endregion
        #region 寝室相关
        public static List<int> GetAllDormStudent(int dormId)   //获取某栋寝室的所有学生编号列表
        {
            //dorId:寝室楼编号
            //返回值：该寝室楼的所有入住学生编号,list里面也可能是空
            DataContext dbContext = new DigitwhuDataContext();
            List<int> result = new List<int>();
            var userId = from u in dbContext.GetTable<UserInfo>()
                         where u.dormId == dormId
                         select u.userId;
            foreach (int u in userId)
            {
                result.Add(u);  //添加到记录
            }
            return result;
        }
        #endregion
        #region 教学楼相关
        public static List<int> GetTeachRoomHavingClass(int buildingId)//查找某栋教学楼的当前的所有上课教室
        {
            //buildingId:教学楼的编号
            //返回值：当前在上课的教室编号列表
            DataContext dbContext = new DigitwhuDataContext();
            ITeachBuildingRepository teach = new TeachBuildingRepository(dbContext);
            return teach.GetTeachRoomHavingClass(buildingId);
        }
        public static List<TeachMessage> GetTeachMessage(int buildingId)    //获得某栋教室的所有留言
        {
            //buildingId:预获取留言的教室的id
            //返回值：所有留言的对象集合
            //返回值：当前在上课的教室编号列表
            DataContext dbContext = new DigitwhuDataContext();
            ITeachBuildingRepository teach = new TeachBuildingRepository(dbContext);
            return teach.GetTeachMessage(buildingId);
        }
        public static bool AddTeachMessage(int buildingId, int userId, string contents)//给教室留言
        {
            //buildingId:教学楼编号
            //userId：要留言的用户编号
            //contents:留言内容
            //返回值：留言是否成功
            DataContext dbContext = new DigitwhuDataContext();
            ITeachBuildingRepository teach = new TeachBuildingRepository(dbContext);
            return teach.AddTeachMessage(buildingId, userId, contents);
        }
        public static TeachMessage GetLatestTeachMessage(int buildingId)    //获取某个教室的最新留言
        {
            //buildingId：预获取最新留言的教学楼编号
            //返回值：最新的留言或者Null
            DataContext dbContext = new DigitwhuDataContext();
            ITeachBuildingRepository teach = new TeachBuildingRepository(dbContext);
            return teach.GetLatestTeachMessage(buildingId);
        }
        #endregion
    }
}